class Solution {
public:
    bool check(vector<int>& a, int x)
    {
        int cnt = 0;
        for (auto z : a) {
            if (z >= x)cnt++;
        }
        return cnt >= x;
    }
    int hIndex(vector<int>& a) {
        int l = -1, r = 1001;
        while (l + 1 < r) {
            int mid = l + r >> 1;
            if (check(a, mid))l = mid;
            else r = mid;
        }
        return l;
    }
};